package com.rongc.client.freight.business.carrier.view.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.core.utils.DateUtil;
import com.rongc.client.core.utils.StringUtils;
import com.rongc.client.freight.ApiUrl;
import com.rongc.client.freight.R;
import com.rongc.client.freight.UniApplication;
import com.rongc.client.freight.base.view.adapter.RecyclerBaseAdapter;
import com.rongc.client.freight.business.supply.model.SupplyBean;

import java.util.List;

/**
 * Created by ccqx on 2017/6/28.
 */

public class RecyclerConveyanceSupplyAdapter extends RecyclerBaseAdapter<SupplyBean> {


    public RecyclerConveyanceSupplyAdapter(Context mContext, List<SupplyBean> mDatas) {
        super(mContext, mDatas);
    }

    @Override
    protected void onBind(final BaseRecyclerViewHolder holder, int position, SupplyBean data) {
        holder.getImageView(R.id.tv_phone).setVisibility(View.INVISIBLE);
        holder.setText(R.id.tv_departure, data.getStart_ssq());
        holder.setText(R.id.tv_destination, data.getEnd_ssq());
        holder.setText(R.id.tv_distance, String.format(mContext.getResources().getString(R.string.supply_list_distance),
                data.getDistance()));
        holder.setText(R.id.tv_total, String.format(mContext.getResources().getString(R.string.supply_waybill_total), data.getFreightByType()));
        holder.setText(R.id.tv_create_date, DateUtil.formatDateStr2Desc(data.getCreate_time(), DateUtil.dateFormatMD));

        holder.setText(R.id.tv_type, data.getGoods());
        holder.setText(R.id.tv_date, data.getLoadTime());
        holder.setText(R.id.tv_model, StringUtils.getNewModel(mContext, data.getCarLength(), data.getWeights(),data.getDilk(), data.getCarModels()));

        holder.getView(R.id.tv_phone).setTag("");
        holder.getView(R.id.tv_phone).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String number = (String) v.getTag();
                ActivityUtils.callTo(mContext,number);
            }
        });

        Glide.with(UniApplication.getInstance())
                .load(ApiUrl.getImgUrl(data.getImg()))
                .asBitmap()
                .centerCrop()
                .placeholder(R.mipmap.universal_head_default)
                .into(new BitmapImageViewTarget(holder.getImageView(R.id.iv_head)) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable roundedBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(mContext.getResources(), resource);
                        roundedBitmapDrawable.setCircular(true);
//                                        roundedBitmapDrawable.setCornerRadius(20); //设置圆角半径（根据实际需求）
                        roundedBitmapDrawable.setAntiAlias(true); //设置反走样
                        getView().setImageDrawable(roundedBitmapDrawable);
                    }
                });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.recycler_conveyance_supply_item;
    }
}
